#include "adminui.h"
#include "ui_adminui.h"
#include "userdb.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QMessageBox>
#include <QDebug>

adminUI::adminUI(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::adminUI)
{
    ui->setupUi(this);
    ui->centralwidget->setFixedSize(1024, 600);
    ui->stackedWidget->setCurrentWidget(ui->page_user_info);
    ui->pushButton_user_info->setFlat(false);
    ui->pushButton_user_clock_in->setFlat(true);
    //初始化用户信息表
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("userdb.db");
    db.open();
    model_user_clock_in = new QSqlTableModel(this, db);
    model_user_info = new QSqlTableModel(this, db);
    model_user_info->setTable("USER_INFO");
    model_user_info->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model_user_info->select();
    model_user_info->setHeaderData(0, Qt::Horizontal, "账号");
    model_user_info->setHeaderData(1, Qt::Horizontal, "用户名");
    model_user_info->setHeaderData(2, Qt::Horizontal, "密码");
    model_user_info->setHeaderData(3, Qt::Horizontal, "电话号码");
    model_user_info->setHeaderData(4, Qt::Horizontal, "身份证");
    model_user_info->setHeaderData(5, Qt::Horizontal, "地址");
    ui->tableView_user_info->setModel(model_user_info);
    ui->tableView_user_info->setSelectionMode(QTableView::SingleSelection);
    ui->tableView_user_info->setSelectionBehavior(QTableView::SelectRows);
    //初始化用户考勤表
    model_user_clock_in->setTable("USER_CLOCK_IN");
    model_user_clock_in->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model_user_clock_in->select();
    model_user_clock_in->setHeaderData(0, Qt::Horizontal, "账号");
    model_user_clock_in->setHeaderData(1, Qt::Horizontal, "打卡日期");
    ui->tableView_user_clock_in->setModel(model_user_clock_in);
    ui->tableView_user_clock_in->setSelectionMode(QTableView::SingleSelection);
    ui->tableView_user_clock_in->setSelectionBehavior(QTableView::SelectRows);
}

adminUI::~adminUI()
{
    delete ui;
}

/**
 * @brief adminUI::on_pushButton_insert_clicked
 * 添加一条用户数据
 */
void adminUI::on_pushButton_insert_clicked()
{
    model_user_info->insertRow(model_user_info->rowCount());
}

/**
 * @brief adminUI::on_pushButton_delete_clicked
 * 删除一条用户数据
 */
void adminUI::on_pushButton_delete_clicked()
{
    QModelIndexList selected = ui->tableView_user_info->selectionModel()->selectedRows();
    foreach (QModelIndex index, selected) {
        model_user_info->removeRow(index.row());
    }
}

/**
 * @brief adminUI::on_pushButton_submit_clicked
 * 提交修改的数据,同步到数据库中
 */
void adminUI::on_pushButton_submit_clicked()
{
    if (!model_user_info->submitAll()) {
        QMessageBox::information(this, "tip", "提交失败");
    }
}


void adminUI::on_pushButton_back_clicked()
{
    emit this->back();
    this->hide();
}


void adminUI::on_pushButton_user_clock_in_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_user_clock_in);
    ui->pushButton_user_info_2->setFlat(true);
    ui->pushButton_user_clock_in_2->setFlat(false);
}


void adminUI::on_pushButton_user_info_2_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->page_user_info);
    ui->pushButton_user_info->setFlat(false);
    ui->pushButton_user_clock_in->setFlat(true);
}

